iT邦幫忙

2024 iThome 鐵人賽

DAY 29
0

將之前實作的所有功能(如轉移、批准、跨鏈轉移集成)整合到同一個合約中,確保它們能夠協同工作。

const MyToken = artifacts.require("MyToken");

contract("MyToken", accounts => {
    let tokenInstance;
    const totalSupply = web3.utils.toWei("1000", "ether"); // 總供應量

    before(async () => {
        tokenInstance = await MyToken.deployed();
    });

    // 測試合約初始化時的總供應量
    it("should allocate the initial supply upon deployment", async () => {
        const supply = await tokenInstance.totalSupply();
        assert.equal(supply.toString(), totalSupply, "Initial supply is incorrect");
    });

    // 測試轉移
    it("should transfer token ownership", async () => {
        const sender = accounts[0];
        const receiver = accounts[1];
        const amount = web3.utils.toWei("100", "ether");

        await tokenInstance.transfer(receiver, amount, { from: sender });
        const balanceReceiver = await tokenInstance.balanceOf(receiver);
        assert.equal(balanceReceiver.toString(), amount, "Receiver should have received 100 tokens");

        const balanceSender = await tokenInstance.balanceOf(sender);
        assert.equal(balanceSender.toString(), totalSupply - amount, "Sender's balance should be reduced");
    });

    // 測試轉移Token時餘額不足的情況
    it("should not allow transfer of more tokens than balance", async () => {
        const sender = accounts[0];
        const receiver = accounts[1];
        const amount = web3.utils.toWei("2000", "ether"); // 大於可用餘額

        try {
            await tokenInstance.transfer(receiver, amount, { from: sender });
            assert.fail("Transfer should have thrown an error");
        } catch (error) {
            assert(error.message.includes("餘額不足"), "Expected '餘額不足' error message");
        }
    });

    // 測試批准功能
    it("should approve tokens for delegated transfer", async () => {
        const owner = accounts[0];
        const spender = accounts[1];
        const amount = web3.utils.toWei("50", "ether");

        await tokenInstance.approve(spender, amount, { from: owner });
        const allowance = await tokenInstance.allowance(owner, spender);
        assert.equal(allowance.toString(), amount, "Allowance should be correctly set");
    });

    // 測試轉移
    it("should transfer tokens on behalf of the owner", async () => {
        const owner = accounts[0];
        const spender = accounts[1];
        const receiver = accounts[2];
        const amount = web3.utils.toWei("30", "ether");

        // 先批准轉移
        await tokenInstance.approve(spender, amount, { from: owner });
        await tokenInstance.transferFrom(owner, receiver, amount, { from: spender });

        const balanceReceiver = await tokenInstance.balanceOf(receiver);
        assert.equal(balanceReceiver.toString(), amount, "Receiver should have received 30 tokens");

        const balanceOwner = await tokenInstance.balanceOf(owner);
        assert.equal(balanceOwner.toString(), totalSupply - amount, "Owner's balance should be reduced");
    });

    // 測試金額的轉移
    it("should not allow transfer from another user beyond allowed amount", async () => {
        const owner = accounts[0];
        const spender = accounts[1];
        const receiver = accounts[2];
        const amount = web3.utils.toWei("100", "ether"); // 大於數量

        try {
            await tokenInstance.transferFrom(owner, receiver, amount, { from: spender });
            assert.fail("Transfer should have thrown an error");
        } catch (error) {
            assert(error.message.includes("餘額不足"), "Expected '餘額不足' error message");
        }
    });

    // 測試銷毀功能
    it("should burn tokens", async () => {
        const owner = accounts[0];
        const burnAmount = web3.utils.toWei("20", "ether");

        const initialSupply = await tokenInstance.totalSupply();
        await tokenInstance.burn(burnAmount, { from: owner });

        const finalSupply = await tokenInstance.totalSupply();
        assert.equal(finalSupply.toString(), initialSupply.sub(burnAmount).toString(), "Total supply should decrease after burning tokens");
    });

    // 測試超過餘額的Token
    it("should not allow burning more tokens than balance", async () => {
        const owner = accounts[0];
        const burnAmount = web3.utils.toWei("1000", "ether"); // 大於可用餘額

        try {
            await tokenInstance.burn(burnAmount, { from: owner });
            assert.fail("Burn should have thrown an error");
        } catch (error) {
            assert(error.message.includes("餘額不足"), "Expected '餘額不足' error message");
        }
    });
});

說明

  1. 合約初始化測試:驗證在部署合約時,初始供應量是否正確。
  2. 轉移Token測試:測試正常的Token轉移和轉移後的餘額更新。
  3. 餘額不足測試:測試在轉移過程中如果餘額不足,是否會觸發錯誤。
  4. 批准功能測試:測試批准功能是否能正確設置授權金額。
  5. 轉移授權Token測試:驗證轉移授權Token的功能是否正常。
  6. 超過授權金額轉移測試:測試在授權金額範圍之外是否能轉移Token。
  7. 銷毀功能測試:測試Token銷毀功能是否能正常工作,並且更新總供應量。

上一篇
Day28 跨鏈轉移的初步架構
下一篇
Day30 總結
系列文
Token相關的應用場景和技術領域30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言